package com.hxzy.config;

import com.hxzy.config.interceptor.DemoInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * @Desc springmvc最核心的配置文件
 * @Author: admin
 * @Date: 2023/08/21/11:31
 */
@Configuration
public class MvcConfig extends WebMvcConfigurationSupport {

    /**
     * 自定义拦截器类
     */
    @Autowired
    private DemoInterceptor  demoInterceptor;

    /**
     * 自定义拦截器
     * @param registry
     */
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
       registry.addInterceptor(demoInterceptor)
               // url地址哪些要拦
               .addPathPatterns("/api/**")
               // url地址哪些要放行
               .excludePathPatterns("/api/login");

    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 解决静态资源无法访问
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/");

        // 解决swagger的js文件无法访问
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

        registry.addResourceHandler("swagger-ui.html", "doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");

    }
}
